
//用户加密id
var uid_encryptid = getCookie("uid_encryptid");
var uid = getCookie("uid");
var uid_name = getCookie("token"); //当前登录的用户名
var admin_role = getCookie("admin_role"); //用户角色
console.log("uid_encryptid:"+uid_encryptid);
console.log("uid:"+uid);
console.log("admin_role:"+admin_role);
console.log("uid_name:"+uid_name);

//每隔7天，删除用户升级为商家，已被拒绝的数据
// setInterval(function () {
//     delbussinessRefuse();
// }, 7*24*60*60*1000);

$(function(){

    if(uid_name != "" && uid_name != undefined){
        $("#uid_name").text(uid_name);
    }

    if(admin_role != "" && admin_role != undefined){
        //菜单列表加载
        var str = "";
        if(admin_role == "2"){
            $("#side").find("li").css("display","block");
            $("#side").find("dd").css("display","block");
        }else if(admin_role == "1"){
            //商家
            // 判断是农家乐商家，还是 景区商家
            getBusinessType(uid_encryptid,admin_role,uid);
        }
    }

    if(uid_encryptid != "" && uid_encryptid != undefined){
        //获取个人信息
        getInformation();
    }

})

//获取用户基本信息
function getInformation() {
    $.post("/userInformation", {
        "user_encryptId": uid_encryptid
    }, function (value) {
        if (value.status == 0){
            var data = value.data;
            $("#username").val(data.userName);//用户名
            $("#nickname").val(data.userRealname);//姓名
            $("#tel").val(data.userTel);//电话
            $("#email").val(data.userMail);//邮箱
        }else{
            if(value.msg==null||value.msg==""){
                layer.alert("接口出现错误！", {icon: 2})
            }else{
                layer.alert(value.msg, {icon: 2})
            }
        }
    });
}

function updatepwd() {
        ComOpenWin(1, "修改密码", "500px", 'auto', $("#updatePwd"), 1);
}

//修改个人信息
function modifypersoninfo(){
	ComOpenWin(1, "修改个人信息", "500px", 'auto', $("#modifypersoninfo"), 1);
}

//保存个人信息
function saveModifyPersonalinfo() {
    var username = $.trim($("#username").val());//用户名
    var nickname = $.trim($("#nickname").val());//姓名
    var tel = $.trim($("#tel").val());//电话
    var email = $.trim($("#email").val());//邮箱
    var layLoad=layer.load(2);
    $.post("/updateInformation", {
        "user_encryptId": uid_encryptid,
        "user_realName":nickname,
        "user_tel":tel,
        "user_mail":email
    }, function (value) {
        layer.close(layLoad);
        if (value.status == 0){
            mclose();
            layer.msg("操作成功！",{icon:1,time:2000});
            getInformation();
        }else{
            if(value.msg==null||value.msg==""){
                layer.alert("接口出现错误！", {icon: 2})
            }else{
                layer.alert(value.msg, {icon: 2})
            }
        }
    });
}

//修改密码
function saveModifyUserPersonalPasswordFunction() {
    var oldPwd = $("#oldPassword").val().trim();
    var newPwd = $("#newPassword").val().trim();
    var newPwd2 = $("#newPassword2").val().trim();
    if($("#newPassword").val().trim() !=  $("#newPassword2").val().trim()){
        layer.msg('两次输入密码不一样，请重新输入', {icon: 0,time:4000});
        $("#newPassword").val("");
        $("#newPassword2").val("");
    }else if((newPwd.length <6 || newPwd.length>16) && (newPwd2.length<6 || newPwd2.length) ){
        layer.msg('密码必须是由6-16个字符组成且至少包含字母、数字、符号任意二项', {icon: 0,time:4000});
    }else{
        var layLoad=layer.load(2);
        $.post("/forceModifyPassword", {
            "user_encryptId": uid_encryptid,
            "oldPwd":oldPwd,
            "newPwd":newPwd
        }, function (value) {
            layer.close(layLoad);
            if (value.status == 0){
                mclose();
                layer.msg("操作成功！",{icon:1,time:2000});
            }else{
                if(value.msg==null||value.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(value.msg, {icon: 2})
                }
            }
        });
    }

}

// 判断是农家乐商家，还是 景区商家
function getBusinessType(uid_encryptid,uid_role,userid) {
    console.log("商家登录……");
    if(uid_encryptid != "" && uid_role != ""){
        var layLoad=layer.load(2);
        $.post("/getuidBusinessType", {
            "user_encryptid": uid_encryptid,
            "user_role":uid_role,
            "userid":userid
        }, function (value) {
            layer.close(layLoad);
            if (value.status == 0){
                var list = value.list;
                if(list.length>0){
                    $("#businessli").show();
                    $(".businesslist").show();
                    if(list[0].business_type == "1"){
                        //农家乐
                        $("#businessfarm").show();
                        $("#businessfarm").find("dd").css("display","block");

                    }else if(list[0].business_type == "2"){
                        //景区
                        $("#businesstour").show();
                        $("#businesstour").find("dd").css("display","block");
                    }
                }
            }else{
                if(value.msg==null||value.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(value.msg, {icon: 2})
                }
            }
        });

    }else {
        layer.alert("uid_encryptid，uid_role不能为空！",{icon:1});
    }
}


/**
 * 定时任务，每隔7天，删除用户升级为商家，已被拒绝的数据
 */
function delbussinessRefuse() {
    $.post('/businesinfo/delbussinessRefuse', function (value) {
        if (value.status == 0) {

        }
    });
}